------------------------------------------------------------------------------------------------------------------------------------------
--[[

    台词库。

    STRINGS.CHARACTERS[string.upper("hutao")] = require "speech_hutao"


]]--
------------------------------------------------------------------------------------------------------------------------------------------
--- 前期准备
    local function test_deepcopy(orig)
        local copy
        -- 检查输入是否是表
        if type(orig) == "table" then
            -- 创建一个新的表来存储复制的内容
            copy = {}
            -- 遍历原表的所有键值对
            for orig_key, orig_value in next, orig, nil do
                -- 对键和值递归调用 deepcopy
                copy[test_deepcopy(orig_key)] = test_deepcopy(orig_value)
            end
            -- 设置新表的元表为原表的元表的深拷贝
            setmetatable(copy, test_deepcopy(getmetatable(orig)))
        else
            -- 如果不是表，直接返回原始值
            copy = orig
        end
        return copy
    end
    local function mergeTables(tableA, tableB)
        -- 创建一个新的表用于存储合并结果
        local result = {}
    
        -- 遍历 tableA 并将所有键值对复制到 result 中
        for key, value in pairs(tableA) do
            if type(value) == "table" then
                -- 如果值是表，则递归处理
                result[key] = mergeTables(value, {})
            else
                -- 否则直接复制值
                result[key] = value
            end
        end
    
        -- 遍历 tableB 并覆盖或添加键值对到 result 中
        for key, value in pairs(tableB) do
            if type(value) == "table" and type(result[key]) == "table" then
                -- 如果两个值都是表，则递归合并
                result[key] = mergeTables(result[key], value)
            else
                -- 否则直接用 tableB 的值覆盖
                result[key] = value
            end
        end
    
        return result
    end
------------------------------------------------------------------------------------------------------------------------------------------

local LANGUAGE = "ch"
if type(TUNING["hutao.Language"]) == "function" then
    LANGUAGE = TUNING["hutao.Language"]()
elseif type(TUNING["hutao.Language"]) == "string" then
    LANGUAGE = TUNING["hutao.Language"]
end

--- for other moders custom language 
TUNING.HUTAO_SPEECH_INIT_FN = TUNING.HUTAO_SPEECH_INIT_FN or function()
    if LANGUAGE == "ch" then
        local custom_speech = require "hutao_speech_ch"
        local willow_speech = test_deepcopy(STRINGS.CHARACTERS.WILLOW)
        STRINGS.CHARACTERS.HUTAO = mergeTables(willow_speech, custom_speech)
    else
        STRINGS.CHARACTERS.HUTAO = test_deepcopy(STRINGS.CHARACTERS.WILLOW)
    end
end

 AddPrefabPostInit(
    "world",
    function(inst)
        inst:DoTaskInTime(0, function()
            TUNING.HUTAO_SPEECH_INIT_FN()
        end)
    end
)